package com.boco.modules.fdoc.web.organization;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;

import org.apache.zookeeper.server.quorum.Leader;
import org.apache.zookeeper.txn.Txn;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.common.utils.PropertiesUtils;
import com.boco.common.utils.StringUtils;
import com.boco.modules.fdoc.model.DoctorEntity;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.model.SysRegionEntity;
import com.boco.modules.fdoc.service.DepartmentService;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.service.HospitalService;
import com.boco.modules.fdoc.service.SysRegionService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;
import com.boco.modules.fdoc.vo.HospitalVo;
import com.qiniu.util.Json;

/**
 * Description: 医院管理controller
 * @author lzz
 *
 */
@Controller
@RequestMapping(value="/hospital",produces = "application/json;charset=UTF-8")
public class HospitalController {
	
	@Resource
	HospitalService hospitalService;
	@Resource
	DocService docService;
	@Resource
	DepartmentService deptService;
	@Resource
	SysRegionService sRegionService;
	
	/**
	 * 跳转医院页面主页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		
		return "/organization/hospital";
	}
	
	/**
	 * 跳转新增医院界面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAddHos",method = RequestMethod.GET)
	public String toAddHos(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		String regionCode="";
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		model.addAttribute("oneList", list);
		return "/organization/addHos";
	}

	/**
	 * 获取最上级的区划
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getOneLevel",method = RequestMethod.GET)
	public String getOneLevel(HttpServletRequest request, Model model){
		//获取最上级的区划
		String regionCode="";
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		return JsonUtils.getJsonFormat(list);
		
	}
	
	/**
	 * 获取当前区划的子区划
	 * @param request
	 * @param model
	 * @param regionCode 当前区划码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getChild",method = RequestMethod.GET)
	public String getChild(HttpServletRequest request, Model model,String regionCode){
		//获取子的区划
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		return JsonUtils.getJsonFormat(list);
		
	}
	
	/**
	 * 获取医院列表 或某一医院的详细信息
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param pageIndex 页码
	 * @param pageSize 页面大小
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getHosList",method = RequestMethod.GET)
	public String getHosList(HttpServletRequest request, Model model,String orgId,Integer pageIndex,Integer pageSize){
		
		HospitalVo hosVo=new HospitalVo();
		hosVo.setId(orgId);
		int count = hospitalService.getCount(hosVo);
		Page<HospitalVo> page=new Page<HospitalVo>(pageIndex,pageSize);
		hosVo.setPage(page);
		List<HospitalVo> list=hospitalService.getHosList(hosVo);
		
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		
		return JsonUtils.getJson(BaseJsonVo.pageList(list, count));
	}
	
	/**
	 * 保存医院信息
	 * @param request
	 * @param model
	 * @param regionCode 区划码
	 * @param orgName 医院名称
	 * @param address 医院地址
	 * @param orgId 医院id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/saveHos",method=RequestMethod.POST)
	public String saveHos(HttpServletRequest request, Model model, String regionCode,String orgName,String address,String orgId){
		
		HospitalEntity hosEntity=new HospitalEntity();
		if(StringUtils.isNoneEmpty(orgId)){
			if(hospitalService.getHospatal(orgId)!=null){
				return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), "此Id已有医院存在！"));
			}
			hosEntity.setId(orgId);
		}else{
			hosEntity.setId(UUID.randomUUID().toString().replace("-", ""));
		}
		
		hosEntity.setRegionCode(regionCode);
		hosEntity.setOrgName(orgName);
		hosEntity.setAddress(address);
		hosEntity.setCreateTime(new Date());
		hosEntity.setUpdateTime(new Date());
		
		
		HospitalEntity existHos=hospitalService.getExistHospatal(hosEntity);
		if(existHos !=null ){
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.HOSPITAL_EXIST.getKey(), ApiStatusEnum.HOSPITAL_EXIST.getValue()));
		}
		
		int flag=hospitalService.saveHos(hosEntity);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
	
	/**
	 * 跳转新增医院
	 * @param request
	 * @param model
	 * @param id 医院id
	 * @return
	 */
	@RequestMapping(value="/toUpdateHos",method=RequestMethod.GET)
	public String toUpdateHos(HttpServletRequest request, Model model, String id){
		
		HospitalEntity hosEntity=hospitalService.getHospatal(id);
		String regionCode=hosEntity.getRegionCode();
		if(hosEntity.getRegionCode()==null || "".equals(hosEntity.getRegionCode())){
			regionCode=PropertiesUtils.getValue("default_region_code");
		}
		String regionCode0;
		String regionCode1;
		String regionCode2;
		String regionCode3;
		List<SysRegionEntity> list1= new ArrayList<SysRegionEntity>();
		List<SysRegionEntity> list2= new ArrayList<SysRegionEntity>();
		List<SysRegionEntity> list3= new ArrayList<SysRegionEntity>();
		List<SysRegionEntity> list4= new ArrayList<SysRegionEntity>();
		if(regionCode.length()>6){
			regionCode0="";
			regionCode1=regionCode.substring(0, 4);
			regionCode2=regionCode.substring(0, 6);
			regionCode3=regionCode.substring(0, 9);
			list1=sRegionService.getChildrenRegions(regionCode0);
			list2=sRegionService.getChildrenRegions(regionCode1);
			list3=sRegionService.getChildrenRegions(regionCode2);
			list4=sRegionService.getChildrenRegions(regionCode3);
		}else{
			regionCode0="";
			regionCode1=regionCode.substring(0, 4);
			regionCode2=regionCode.substring(0, 6);
			regionCode3=null;
			list1=sRegionService.getChildrenRegions(regionCode0);
			list2=sRegionService.getChildrenRegions(regionCode1);
			list3=sRegionService.getChildrenRegions(regionCode2);
			list4=null;
		}
		
		
		
		model.addAttribute("id", id);
		model.addAttribute("hosEntity", hosEntity);
		
		model.addAttribute("oneList", list1); 
		model.addAttribute("twoList", list2);
		model.addAttribute("threeList", list3);
		model.addAttribute("fourList", list4);
		
		model.addAttribute("one", regionCode1);
		model.addAttribute("two", regionCode2);
		model.addAttribute("three", regionCode3);
		model.addAttribute("four", hosEntity.getRegionCode());
		return "/organization/updateHos";
		
	}
	
	/**
	 * 修改医院信息
	 * @param request
	 * @param model
	 * @param id
	 * @param regionCode
	 * @param orgName
	 * @param address
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateHos",method=RequestMethod.POST)
	public String updateHos(HttpServletRequest request, Model model, String id, String regionCode,String orgName,String address){
		
		HospitalEntity hosEntity=new HospitalEntity();
		hosEntity.setRegionCode(regionCode);
		hosEntity.setOrgName(orgName);
		hosEntity.setAddress(address);
		hosEntity.setUpdateTime(new Date());
		hosEntity.setId(id);
		HospitalEntity oldHos=hospitalService.getHospatal(id);
		HospitalEntity existHos=hospitalService.getExistHospatal(hosEntity);
		if(!regionCode.equals(oldHos.getRegionCode() )|| !orgName.equals(oldHos.getOrgName())){
			if( existHos !=null ){
				return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.HOSPITAL_EXIST.getKey(), ApiStatusEnum.HOSPITAL_EXIST.getValue()));
			}
		}
		int flag=hospitalService.updateHos(hosEntity);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
	
	/**
	 * 删除医院
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteHos",method=RequestMethod.POST)
	public String deleteHos(HttpServletRequest request, Model model, String id){
		
		int flag=hospitalService.deleteHos(id);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
}
